import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _class, _class2;

import React, { cloneElement, isValidElement } from 'react';
import PropTypes from 'prop-types';
import { Bind } from 'lodash-decorators';
import { PENDING, SUCCESS } from "../Status";
var Field = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), (_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Field, _React$Component);

  var _super = _createSuper(Field);

  function Field() {
    var _this;

    _classCallCheck(this, Field);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      status: PENDING,
      controllerType: 'disabled'
    };
    return _this;
  }

  _createClass(Field, [{
    key: "componentDidMount",
    value: // 在 render 之前检查权限
    // eslint-disable-next-line
    function componentDidMount() {
      var permissionList = this.props.permissionList;

      if (permissionList !== undefined && Array.isArray(permissionList)) {
        this.check(this.props, this.context);
      }
    }
    /**
     * 调用 context 的 check
     * @param {object} props - 检查所需参数
     * @param {object} context - 上下文
     */

  }, {
    key: "check",
    value: function check(props, context) {
      var _props$permissionList = props.permissionList,
          permissionList = _props$permissionList === void 0 ? [] : _props$permissionList;

      if (context.permission) {
        context.permission.check({
          permissionList: permissionList
        }, this.handlePermission);
      }
    }
    /**
     * 检查权限后的回调函数
     * @param {number} status - 权限状态
     * @param {string} controllerType - 权限的控制类型
     */

  }, {
    key: "handlePermission",
    value: function handlePermission(status) {
      var controllerType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'disabled';
      this.setState({
        status: status,
        controllerType: controllerType
      });
    }
    /**
     * 递归遍历 react 节点
     * @param {array} children - react node
     * @param {function} fn - 回调函数
     */

  }, {
    key: "mapReactChildren",
    value: function mapReactChildren(children, fn) {
      var _this2 = this;

      return React.Children.map(children, function (child) {
        if (!isValidElement(child)) {
          return child;
        }

        if (child.props.children) {
          // eslint-disable-next-line
          child = React.cloneElement(child, {
            children: _this2.mapReactChildren(child.props.children, fn)
          });
        }

        return fn(child);
      });
    }
  }, {
    key: "extendProps",
    value: function extendProps() {
      var _this$props = this.props,
          permissionList = _this$props.permissionList,
          children = _this$props.children;
      var _this$state = this.state,
          status = _this$state.status,
          controllerType = _this$state.controllerType;

      if (!permissionList || !Array.isArray(permissionList)) {
        return children;
      }

      if (status === SUCCESS) {
        if (controllerType === 'disabled') {
          return this.mapReactChildren(children, function (child) {
            return cloneElement(child, {
              disabled: true
            });
          });
        } else {
          // approved=true，则controllerType=disabled则禁用，其他，则不控制
          return children;
        }
      } else {
        return null;
      }
    }
  }, {
    key: "render",
    value: function render() {
      return this.extendProps();
    }
  }]);

  return Field;
}(React.Component), _class2.contextTypes = {
  permission: PropTypes.object
}, _class2), (_applyDecoratedDescriptor(_class.prototype, "check", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "check"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handlePermission", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handlePermission"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "mapReactChildren", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "mapReactChildren"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "extendProps", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "extendProps"), _class.prototype)), _class));
export { Field as default };